<?php
/**
 * Occupant actions.
 *
 * @package    utility
 * @subpackage Occupant
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class OccupantActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('Occupant', 'list');
  }
  
  public function executeList()
  {
	$c = new Criteria();
	$c->add(OccupantPeer::STATUS, Constant::RECORD_STATUS_ACTIVE);
	$c->add(OccupantPeer::OCCUPIED_STATUS, Constant::BUILDING_OCCUPIED);
	$c->addAscendingOrderByColumn(OccupantPeer::STATION_ID);
	$c->addAscendingOrderByColumn(OccupantPeer::BUILDING_ID);
	$this->occupants = OccupantPeer::doSelectJoinAll ($c);
	
  } // - END - executeList 

public function executeAdd(sfWebRequest $request)
  {
  	if ($request->isMethod('Post'))
  	{
		if ($this->getRequestParameter('building_id') == NULL)
		{
		$this->getUser()->setFlash('ERROR_MESSAGE', 'Building cannot be Empty. If you want to add Occupant, either add new Buliding or Vacate an existing Building.');
		$this->redirect ('Occupant/list');
		
		}
		//getting consumer no from Building table
		$building = BuildingPeer::retrieveByPk($this->getRequestParameter('building_id'));
		$consumer_no = $building->getConsumerNo();
		
		$occupant = new Occupant();
		
		$occupant->setStationId($this->getRequestParameter('station_id'));
		$occupant->setBuildingId($this->getRequestParameter('building_id'));
		$occupant->setConsumerNo($consumer_no);
		$occupant->setArmyNo($this->getRequestParameter('army_no'));
		$occupant->setRank($this->getRequestParameter('rank'));
		$occupant->setName($this->getRequestParameter('name'));
		$occupant->setUnit($this->getRequestParameter('unit'));
		$occupant->setDateOccupied($this->getRequestParameter('date_occupied'));
		$occupant->setOccupiedStatus(Constant::BUILDING_OCCUPIED);
		$occupant->setStatus(Constant::RECORD_STATUS_ACTIVE);
		
		$occupant->save();
		$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_ADDED_SUCCESSFULLY);
		$this->redirect ('Occupant/list');
	} //end if
	
	else
	{
		$this->Station = array();
		$this->Building = array();
		
		foreach (StationPeer::GetStation() as $p)
		{
			$this->Station[$p->getId()] = $p->getTitle(); 
		}
		
		foreach (BuildingPeer::GetVacantBuilding() as $s)
		{
			$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
		}
	}
  } // - END - executeAdd
  
 
public function executeEdit (sfWebRequest $request)
	{
		if ($request->isMethod('Post'))
		{
			//getting consumer no from Building table
			$building = BuildingPeer::retrieveByPk($this->getRequestParameter('building_id'));
			$consumer_no = $building->getConsumerNo();

			
			$occupant = OccupantPeer::retrieveByPk($this->getRequestParameter('id'));

			$occupant->setStationId($this->getRequestParameter('station_id'));
			$occupant->setBuildingId($this->getRequestParameter('building_id'));
			$occupant->setConsumerNo($consumer_no);
			$occupant->setArmyNo($this->getRequestParameter('army_no'));
			$occupant->setRank($this->getRequestParameter('rank'));
			$occupant->setName($this->getRequestParameter('name'));
			$occupant->setUnit($this->getRequestParameter('unit'));
			$occupant->setDateOccupied($this->getRequestParameter('date_occupied'));
			$occupant->setOccupiedStatus($this->getRequestParameter('occupied_status'));
			
			//Save object to database
			if($occupant->save())
			{
				$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_EDITED_SUCCESSFULLY);
				$this->redirect ('Occupant/list');
			}
			else
			{
				$this->getUser()->setFlash('ERROR_MESSAGE', Constant::DB_ERROR);
				$this->redirect('Occupant/edit?id='.Utility::EncryptQueryString($request->getParameter('id')));
			}
		}

		else
		{
			$this->occupant = OccupantPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
			
			$this->Station = array();
			$this->Building = array();
			
			foreach (StationPeer::GetStation() as $p)
			{
				$this->Station[$p->getId()] = $p->getTitle(); 
			}
			
			foreach (BuildingPeer::GetVacantBuilding() as $s)
			{
				$this->Building[$s->getId()] = array($s->getStationId(), $s->getBuildingNo()); 
			}
		}
	} //- END - executeEdit
  
public function executeDelete (sfWebRequest $request)
	{
		$occupant = OccupantPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));

		if($occupant)
		{
			$occupant->setStatus(Constant::RECORD_STATUS_DELETED);
			$occupant->setOccupiedStatus(Constant::BUILDING_VACANT);
			if(	$occupant->save())
			$this->getUser()->setFlash('SUCCESS_MESSAGE', Constant::RECORD_STATUS_DELETED_SUCCESSFULLY);
			else
			$this->getUser()->setFlash('ERROR_MESSAGE', Constant::DB_ERROR);
		}
		else
		{
			$this->getUser()->setFlash('ERROR_MESSAGE', Constant::INVALID_REQUEST);
		}
	
		$this->redirect ('Occupant/list');
	} //- END - executeDelete
	
	public function executeVacateBuilding(sfWebRequest $request)
	{
		if ($request->isMethod('Post'))
		{
			$date_vacated = $request->getParameter('date_vacated');
			$occupant = OccupantPeer::retrieveByPk($request->getParameter('occupant_id'));
		
			if($occupant)
			{
				$occupant->setOccupiedStatus(Constant::BUILDING_VACANT);
				$occupant->setDateVacated($date_vacated);
				if(	$occupant->save())
				$this->getUser()->setFlash('SUCCESS_MESSAGE', 'Building Vacated Successfully. You can assign new Occupant now.');
				else
				$this->getUser()->setFlash('ERROR_MESSAGE', Constant::DB_ERROR);
			}
			else
			{
				$this->getUser()->setFlash('ERROR_MESSAGE', Constant::INVALID_REQUEST);
			}
	
			$this->redirect ('Occupant/list');
		}
		
		else
		{
			$this->occupant = OccupantPeer::retrieveByPk(Utility::DecryptQueryString($request->getParameter('id')));
		}
	} //- END -  executeVacateBuilding

} // END Class
